// JSDoc


/**
 * 日期格式化
 * @author @zhan-zhuhong
 * @param {String} time    判断的时间戳：2021-11-25 7:20:20
 * @return String
 */

function Formatime(time){
    var nowDate = (new Date).getTime() / 1000    //获取当前时间的时间戳
    var oldDate = new Date(time).getTime() /1000
    var distance = nowDate - oldDate
    var rs = null

if ( distance < 60){
    rs =  "刚刚发布"
 } else if ( distance >= 60 && distance < 3600){
    rs =     (distance / 60) + '分钟前发布' 
 }else if (distance >=3600 && distance < 86400){
    rs =    parseInt(  distance/ 60 /60 ) + "小时前发布"
    
 }else  if (distance >= 86400 ){
    rs =    parseInt(  distance / 60 / 60 /24) + "天前发布"
 }
  console.log(rs);
  
}
// Formatime('2021-11-25 7:20:20')   



/**
 * 判断浏览器内核
 * @author @zhan-zhuhong
 * @return  object
 */
function explorer(){
    function liulanqi() {
    var u = navigator.userAgent;
    return {
            trident: u.indexOf('Trident') > -1, //IE内核
            presto: u.indexOf('Presto') > -1, //opera内核
            webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
            gecko: u.indexOf('Firefox') > -1, //火狐内核Gecko
            mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
            ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios
            android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android
            iPhone: u.indexOf('iPhone') > -1 , //iPhone
            iPad: u.indexOf('iPad') > -1, //iPad
            webApp: u.indexOf('Safari') > -1 //Safari
        }
    }
console.log(liulanqi())
}
//explorer()


/**
 * 判断是否是移动端
 * @author @zhan-zhuhong
 * @return  boolear
 */
var browser = {
    versions: function() {
      var u = navigator.userAgent;
      return {
              mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
              ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios
              android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android
              iPhone: u.indexOf('iPhone') > -1 , //iPhone
              iPad: u.indexOf('iPad') > -1, //iPad
              webApp: u.indexOf('Safari') > -1 //Safari
          };
      }
  }
 /*  if (browser.versions.mobile || browser.versions.ios || browser.versions.android || browser.versions.iPhone || browser.versions.iPad) {
      alert('移动端');
  }else(
      alert('PC')
  ) */



  /**
 * 把Json对象转换为JS对象
 * @author @zhan-zhuhong
 * @return  object
 */
//自己封装一个parse将后端给的数据转换为前端对象
function parse(javaData){
    let temp = {}
    //let javaData = '{"uname":"宇宙","age":"18"}'
//先把json数据格式的 {}," 去掉
javaData = javaData.replace(/{|}|"/g,'') 
//console.log(javaData);   uname:宇宙,age:18

//变数组:split是根据指定字符串分割字符成数组 ==>  javaDataArr[0] = 'uname:宇宙', javaDataArr[1]= 'age:18'
 javaDataArr = javaData.split(',')
//console.log(javaData);   ['uname:宇宙', 'age:18']

//遍历数组
javaDataArr.forEach(item =>{
    // console.log(item);  uname:宇宙   age:18
    //根据冒号将item分隔为数组 ==>
    let  newArr = item.split(':')
    //console.log(newArr);
    let key = newArr[0]
    let value = newArr[1]
    temp[key] = value
})
return temp
}
//console.log(parse('{"uname":"宇宙","age":"18"}'));



  /**
 * 把js对象转为JSON对象
 * @author @zhan-zhuhong
 * @return  String
 */
function stringify(webObj){
    //let webObj ={uname: '宇宙', age: '18'}
    let arr = []
    // 1 遍历对象
    for (let key in webObj)
    {
        // 2 遍历外面-定义空数组arr
        // 3 遍历里面-将  键拼接:拼接值 push到数组中
        // let temp = `"键":"值"`
        let temp = `"${key}":"${webObj[key]}"`
        // 4 遍历结束-将数组转字符串即可
        arr.push(temp)
    }
     //console.log(`{${arr}}`);
    return `{${arr}}`
}
//console.log(stringify({uname: '宇宙', age: '18'}));